//
//  HttpConnect.m
//  MyFamework
//
//  Created by carlos on 12-10-3.
//  Copyright (c) 2012年 carlos. All rights reserved.
//

#import "HttpConnect.h"
#import "ASIFormDataRequest.h"


@interface HttpConnect()

@end

@implementation HttpConnect
//单例模式
+ (HttpConnect *)instance{
    static HttpConnect *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[HttpConnect alloc]init];
        [instance initContentConfig];
    });
    return instance;
}
//初始化配置
- (void) initContentConfig{
    [ASIHTTPRequest setDefaultTimeOutSeconds:6];
}

//连接
- (void)connent:(HttpEnty *)httpEnty withThreadMessage:(ThreadMessage *)threadMessage{
    NSMutableString *urlString = [NSMutableString string];
    [urlString appendString:httpEnty.url];
    //如果键值对存在，则填充
    NSString *encodeUrl = [NSString stringWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    NSURL *baseUrl = [NSURL URLWithString:encodeUrl];
    
    if (kCLogShowHttp) {
        CLog(@"\n baseUrl ==%@",baseUrl);
    }

    //连接网络
    ASIHTTPRequest *request = nil;

    
    switch (httpEnty.httpMethod) {
        case GET:
        {
            NSEnumerator *terator = httpEnty.params.keyEnumerator;
            NSString *key = nil;
            NSMutableString *urlString = [NSMutableString stringWithFormat:@"%@=",encodeUrl];
            if ((key = terator.nextObject)) {
                NSString *value = httpEnty.params[key];
                [urlString appendFormat:@"%@=%@",key,value];
            }
            request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlString]];
            break;
        }
        case POST:{
            ASIFormDataRequest *request1 = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:encodeUrl]];

            NSEnumerator *terator = httpEnty.params.keyEnumerator;
            NSString *key = nil;
            if ((key = terator.nextObject)) {
                NSString *value = httpEnty.params[key];
                [request1 setPostValue:value forKey:key];
            }
            request = request1;
            break;
        }
        default:
            break;
    }

//    [request setValuesForKeysWithDictionary:httpEnty.params];
    [request startSynchronous];
    
    //                              CLog(@"%@",[response valueForKey:@"result"]);
    if (request.error) {
        if (_delegate && [_delegate respondsToSelector:@selector(httpConnectFail:withThreadMessage:)]) {
            [_delegate httpConnectFail:request.error withThreadMessage:threadMessage];
        }
    }else{
        NSString* content = [[NSString alloc] initWithData:request.responseData encoding:NSUTF8StringEncoding];
        
        if (kCLogShowHttp) {
            CLog(@"content = %@",content);
            //        CLog(@"header = %@",[request ]);
        }
        if (_delegate && [_delegate respondsToSelector:@selector(httpConnectSuccess:withThreadMessage:)]) {
            
            [_delegate httpConnectSuccess:content withThreadMessage:threadMessage];
        }
    }
}
@end
